package cn.qf.dao.impl;

import cn.qf.dao.UserDao;

import cn.qf.pojo.User;
import cn.qf.utils.DruidJbdcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User selectAllUser() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql="select * from user";
        List<User> list = queryRunner.query(sql, new BeanListHandler<User>(User.class));
        //BeanListHandler是将集合中每一条数据封装到指定的Javabean中，将这些javabean封装到list集合中
        return null;
    }

    @Override
    public User selectUserById(String uid) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql="select * from user where uid=?";
        User user=queryRunner.query(sql,new BeanHandler<User>(User.class),uid);
        return null;
    }

    @Override
    public int update(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql = "update user set username = ? , password= ?, name = ?,telephone=?,birthday=?,sex=? where uid = ?" ;
        int count = queryRunner.update(sql, user.getUsername(), user.getPassword(),
                user.getName(),
                user.getTelephone(),
                user.getBirthday(),
                user.getSex(),
                user.getUid());
        return 0;
    }

    @Override
    public int deletUserById(String uid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(DruidJbdcUtils.getDataSource());
        String sql="delect from user where uid=?";
        int count=queryRunner.update(sql,uid);
        return 0;
    }

}
